796A - Buying A House - CodeForces Solution


brute force implementation *800

Please click on ads to support us..

Python Code:

n,m,k=map(int,input().split())
r=[*map(int,input().split())]
mini=10000
for i in range(n):
 if r[i] and k>=r[i]:mini=min(mini,abs(m-i-1)*10)
print(mini)

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define int long long int

signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,k;
cin>>n>>m>>k;
int a[n];
for(int i=0;i<n;i++){
    cin>>a[i];

} 
int ans=-1;
if((m-1)==0 || m==n){
    if(m-1==0){
        int pri=m;
        while(pri!=n){
            if(a[pri]!=0 && a[pri]<=k){ans=pri;
                break;
            }
            else pri++;
        }
          cout<<(abs(m-ans)+1)*10;
    return 0;
    }
    else if(m==n){
        int pri=m-2;
         while(pri>=0){
            if(a[pri]!=0 && a[pri]<=k){
                ans=pri;
                break;
            }
            else pri--;
        }
    }
    cout<<(abs(m-ans)-1)*10;
    return 0;
}

else{
int st=m-2,en=m;
   while(st>=0 || en!=n){
       if(a[st]!=0 && a[st]<=k){
            ans=st;
            break;
       }
       if(a[en]!=0 && a[en]<=k){
        ans=en;
        break;
       }
       else {
        if(st!=0)st--;
        if(en!=n)en++;
       }
   }

cout<<abs(m-ans-1)*10;
}

}


Comments

Submit
0 Comments
More Questions

Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party